using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using flickr.api;
using FlickrShots.Framework.Windows;

namespace FlickrShots.Framework
{
    /// <summary>
    /// Can take a downloaded image and convert it to a wallpapr based
    /// on user preferences.
    /// </summary>
	public class Wallpapr
	{
        private Photo _photo;
        private string _wallpaperFolder = @"C:\temp\flickrShots";
        private ImageFormat _imageFormat = ImageFormat.Jpeg;

		internal Wallpapr(Photo photo)
		{
            _photo = photo;
		}

        public void SetAsDesktopWallpaper()
        {
            string fileName = string.Format(@"{0}\{1}.{2}", _wallpaperFolder, _photo.FileName, _imageFormat.ToString());
            
            if (fileName != GetCurrentWallpaperFileName())
            {
             if (!Directory.Exists(_wallpaperFolder))
                Directory.CreateDirectory(_wallpaperFolder);
                
               WallpaperImage.Save(fileName, _imageFormat);

                if (_imageFormat == ImageFormat.Bmp)
                {
                    User32.SystemParametersInfo(
                        User32.SetWallpaper,
                        fileName.Length,
                        fileName,
                        User32.SendChange);
                }
                else if (_imageFormat == ImageFormat.Jpeg)
                {
                    IActiveDesktop desktop = (IActiveDesktop)new ActiveDesktop();

                    WALLPAPEROPT options = new WALLPAPEROPT();
                    options.dwSize = WALLPAPEROPT.SizeOf;
                    options.dwStyle = Preferences.Instance.WallpaperPosition;
                    desktop.SetWallpaperOptions(ref options, 0);

                    desktop.SetWallpaper(fileName, 0);
                    desktop.ApplyChanges(AD_APPLY.ALL);
                }
            }
        }

        public static string GetCurrentWallpaperFileName()
        {
            string name = new string(' ', 260);
            User32.SystemParametersInfo(
                User32.GetWallpaper, 
                name.Length,
                name,
                User32.NoChanges);
            int stringTerminatorPosition = name.IndexOf('\0');
            return name.Remove(stringTerminatorPosition, name.Length - stringTerminatorPosition);
        }
        
        public string Title
        {
            get { return _photo.Title; }
        }
        
        [Browsable(false)]
        public Image WallpaperImage
        {
            get { return _photo.OriginalImage; }
        }
        
        [Browsable(false)]
        public Image PreviewImage
        {
            get { return _photo.SquareThumbnail; }
        }
        
        public string DatePosted
        {
            get { return _photo.Info.Dates.PostedDate.ToLocalTime().ToString(); }
        }
        public string DateTaken
        {
            get { return _photo.Info.Dates.TakenDate.ToString(); }
        }
        
        public string Owner
        {
            get { return _photo.OwnerName;  }
        }
        
        public string Description
        {
            get { return _photo.Info.Description; }
        }
        
        public string[] Tags
        {
            get
            {
                int i = 0;
                PhotoInfoTags tags = _photo.Info.Tags;
                if (tags.TagCollection != null)
                {
                    string[] tagArray = new string[tags.TagCollection.Length];
                    foreach (PhotoInfoTag tag in tags.TagCollection)
                    {
                        tagArray[i++] = tag.TagText;
                    }
                    return tagArray;
                }
                return null;
            }
        }
        
	}
}